package edu.bbu.commonproject.desktop.gui.view.swing.file.traffic;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.event.ComponentAdapter;
import java.awt.event.ComponentEvent;
import java.awt.event.MouseAdapter;
import java.lang.reflect.InvocationTargetException;

import javax.swing.BorderFactory;
import javax.swing.ImageIcon;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.SwingUtilities;

import edu.bbu.commonproject.desktop.gui.view.swing.file.traffic.utils.ListContainer;
import edu.bbu.commonproject.desktop.gui.view.swing.file.traffic.utils.ListHeader;
import edu.bbu.commonproject.desktop.gui.view.swing.file.traffic.utils.ListHeaderCorner;
import edu.bbu.commonproject.desktop.gui.view.swing.file.traffic.utils.TrafficControllerPanel;
import edu.bbu.commonproject.desktop.gui.view.swing.utils.Design;

/**
 * The scroll list.
 */
public class ScrollList extends JPanel
{
	/** It initialises the component with. */
	private int componentWidth = 185;
	
	/** The scroll panel. */
	private JScrollPane scrollPane;
	
	/** The container list. */
	private ListContainer containerList;
	
	/** The header item. */
	private ListHeader headerItem;
	
	/** The header corner. */
	private ListHeaderCorner headerCorner;
	
	/** The traffic control panel. */
	private TrafficControllerPanel trafficControllerPanel;
	
	/**
	 * The constructor.
	 */
	public ScrollList()
	{
		containerList = new ListContainer();
		containerList.setTexture(Design.TRAFFIC_COMPONENT_BASETEXTURE);
		
		headerItem = new ListHeader();
		headerCorner = new ListHeaderCorner();
		
		scrollPane = new JScrollPane(containerList);
		scrollPane.setColumnHeaderView(headerItem);
		scrollPane.getVerticalScrollBar().setUnitIncrement(20);
		scrollPane.setCorner(JScrollPane.UPPER_RIGHT_CORNER, headerCorner);
		scrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);	
		
		trafficControllerPanel = new TrafficControllerPanel();
		
		this.setLayout(new BorderLayout());
		this.addComponentListener(new ComponentAdapter()
		{
			public void componentResized(ComponentEvent e)
			{
				autoResize();
			}
		});
		
		this.add(scrollPane, BorderLayout.CENTER);
		this.add(trafficControllerPanel, BorderLayout.SOUTH);
		
	}
	
	/**
	 * Sets the preferred size.
	 */
	private void autoResize()
	{
		this.setPreferredSize(new Dimension(scrollPane.getWidth(), componentWidth));
		this.revalidate();
	}
	
	/**
	 * It adds the item.
	 * @param item the item.
	 */
	public void addItem(final ListItem item)
	{
		try
		{
			SwingUtilities.invokeAndWait(new Runnable()
			{
				public void run()
				{
					containerList.addListItem(item);
					containerList.revalidate();
					containerList.repaint();
				}
			});
		}
		catch (InvocationTargetException | InterruptedException e) { }
	}
	
	/**
	 * It removes the item.
	 * @param item the item.
	 */
	public void removeItem(final ListItem item)
	{
		SwingUtilities.invokeLater(new Runnable()
		{
			public void run()
			{
				containerList.removeItem(item);
				containerList.revalidate();
				containerList.repaint();
			}
		});
	}
	
	/**
	 * It destroys all the items.
	 */
	public void destroyAllItems()
	{
		SwingUtilities.invokeLater(new Runnable()
		{
			public void run()
			{
				for (ListItem item : containerList.getAllItems())
				{
					item.stopTransfer();
					containerList.removeItem(item);
				}
				
				containerList.revalidate();
				containerList.repaint();
			}
		});
	}
	
	/**
	 * It sets the component with.
	 * @param componentWidth the component with.
	 */
	public void setComponentWidth(int componentWidth)
	{
		this.componentWidth = componentWidth;
	}
	
	/**
	 * It gets the component with.
	 * @return the component with.
	 */
	public int getComponentWidth()
	{
		return componentWidth;
	}
	
	/**
	 * It gets the selected item.
	 * @return the selected item.
	 */
	public ListItem getSelectedItem()
	{
		return containerList.getSelectedObject();
	}
	
	/**
	 * It gets the treffic control panel.
	 * @return the control panel.
	 */
	public TrafficControllerPanel getControllerPanel()
	{
		return trafficControllerPanel;
	}
}

